#!/bin/bash

# @TODO
command -v os::ensure_permission >>/dev/null
if [ "$?" != "0" ]; then
  # echo "load os::ensure_permission"
  . $ZMICRO_HOME/core/os
fi

command -v log::init >>/dev/null
if [ "$?" != "0" ]; then
  . $ZMICRO_CORE_PATH/log
  log::init
fi

os::ensure_logs_permission

load() {
  # ensure_permission() {
  #   local path=$1
  #   if [ -f $path ] && [ ! -w $path ]; then
  #     sudo chown -R $(os::username) $path
  #   fi
  # }

  # ensure_permission $ZMICRO_LOG_DEBUG_PATH
  echo -e "$(date "+%Y-%m-%d %H:%M:%S")[CORE] load $1" | tee -a $ZMICRO_LOG_DEBUG_PATH >>/dev/null

  local FN_PATH=$ZMICRO_CORE_PATH/$1

  if [ -f $FN_PATH ]; then
    . $FN_PATH
  elif [ -d $FN_PATH ]; then
    . $FN_PATH/export
  fi
}

load require
load lock
load type
load unit
load os
load fs
load net
load color
load path
load timestamp
load delay
load permission
load help
load version
load log
load config
load commander
load plugin
load pm
load service

# Cause Bug
#   1. tput: No value for $TERM and no -T specified
#   2. tput: terminal attributes: No such device or address
#   3. terminal is not a tty
# load inquirer

load run

export -f load
